package com.sx.micowell.module.more

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.mvvm.base.BaseActivity
import com.example.mvvm.base.data.DataBindingConfig
import com.example.mvvm.until.LogUtils
import com.sx.micowell.BR
import com.sx.micowell.R
import com.sx.micowell.databinding.FragMoreRecommendCourseListBinding
import com.sx.micowell.databinding.FragRecommendCourseBinding
import com.sx.micowell.framework.adapter.CourseListAdapter
import com.sx.micowell.framework.app.IntentKey
import com.sx.micowell.framework.app.MyFragment
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.arouth.RoutePath
import com.sx.micowell.framework.arouth.RoutePath.Companion.RECOMMEND_COURSE_LIST
import com.sx.micowell.framework.model.HomeModel
import com.sx.micowell.framework.model.RecommendCourseModel
import com.sx.micowell.module.MainActivity

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/8/25 9:26
 */
@Route(path = RoutePath.MORE_RECOMMEND_COURSE_LIST)
class MoreChildCourseListFragment : MyFragment<FragMoreRecommendCourseListBinding, BaseActivity<*>>() {
    @Autowired(name = IntentKey.TYPE)
    lateinit var type: String

    private val viewModel: RecommendCourseModel by lazy {
        getFragmentScopeViewModel(RecommendCourseModel::class.java)
    }

    private val MainviewModel: HomeModel by lazy {
        getActivityScopeViewModel(HomeModel::class.java)
    }

    override fun getDataBindingConfig(): DataBindingConfig =
        DataBindingConfig(R.layout.frag_more_recommend_course_list, BR.viewModel, viewModel)
            .addBindingParam(BR.adapter,
                CourseListAdapter(context).also {
                    it.setOnItemClickListener { viewId, item, position ->
                        LogUtils.d("CourseListAdapter: ${item.id}")
                        startContainerActivity(
                            RouteManager.getInstance().getCourseVideoFragment(item.id.toString()).javaClass.canonicalName,
                            Bundle().also { it.putString(IntentKey.KEY, item.id.toString()) }
                        )
                    }
                }
            )


    override fun initView() {

    }

    override fun initData() {
        viewModel.reqCourseList(type)
    }

    override fun initViewModel() {
        super.initViewModel()
        MainviewModel.refreshLiveData.observe(this){
            if(it){
                viewModel.reqCourseList(type)
            }
        }
        viewModel.courseList.observe(this) {
            binding.recyclerView.visibility = if (it.size == 0) View.GONE else View.VISIBLE
            findViewById<View>(R.id.empty).visibility =
                if (it.size != 0) View.GONE else View.VISIBLE
        }
    }
}